home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-10-08 | 14.9 KB | 101 lines | [TEXT/R*ch] |
- # Domain Name Server (DNS) interface
- #
- # See RFC 1035:
- # ------------------------------------------------------------------------
- # Network Working Group P. Mockapetris
- # Request for Comments: 1035 ISI
- # November 1987
- # Obsoletes: RFCs 882, 883, 973
- #
- # DOMAIN NAMES - IMPLEMENTATION AND SPECIFICATION
- # ---------------------------------------------------------- dnslib.py',
- print '[-T] [-r] [-s server] [-t] [-u]',
- print '[qtype [qname]]'
- print '-T: run testpacker() and exit'
- print '-r: recursion desired (default not)'
- print '-s server: use server (default %s)' % server
- print '-t: use TCP protocol'
- print '-u: use UDP protocol (default)'
- print 'qtype: query type (default %s)' % \
- dnstype.typestr(qtype)
- print 'qname: query name (default %s)' % qname
- print 'Recognized qtype values:'
- qtypes = dnstype.typemap.keys()
- qtypes.sort()
- n = 0
- for qtype in qtypes:
- n = n+1
- if n >= 8: n = 1; print
- print '%s = %d' % (dnstype.typemap[qtype], qtype),
- print
- sys.exit(2)
- for o, a in opts:
- if o == '-T': testpacker(); return
- if o == '-t': protocol = 'tcp'
- if o == '-u': protocol = 'udp'
- if o == '-s': server = a
- if o == '-r': rd = 1
- if args[0:]:
- try:
- qtype = eval(string.upper(args[0]), dnstype.__dict__)
- except (NameError, SyntaxError):
- print 'bad query type:', `args[0]`
- sys.exit(2)
- if args[1:]:
- qname = args[1]
- if qtype == dnstype.AXFR:
- print 'Query type AXFR, protocol forced to TCP'
- protocol = 'tcp'
- print 'QTYPE %d(%s)' % (qtype, dnstype.typestr(qtype))
- m = Mpacker()
- m.addHeader(0,
- 0, opcode, 0, 0, rd, 0, 0, 0,
- 1, 0, 0, 0)
- m.addQuestion(qname, qtype, dnsclass.IN)
- request = m.getbuf()
- if protocol == 'udp':
- s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- s.connect((server, port))
- s.send(request)
- reply = s.recv(1024)
- else:
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.connect((server, port))
- s.send(pack16bit(len(request)) + request)
- s.shutdown(1)
- f = s.makefile('r')
- header = f.read(2)
- if len(header) < 2:
- print '*** EOF ***'
- return
- count = unpack16bit(header)
- reply = f.read(count)
- if len(reply) != count:
- print '*** Incomplete reply ***'
- return
- u = Munpacker(reply)
- dumpM(u)
- if protocol == 'tcp' and qtype == dnstype.AXFR:
- while 1:
- header = f.read(2)
- if len(header) < 2:
- print '========== EOF =========='
- break
- count = unpack16bit(header)
- if not count:
- print '========== ZERO COUNT =========='
- break
- print '========== NEXT =========='
- reply = f.read(count)
- if len(reply) != count:
- print '*** Incomplete reply ***'
- break
- u = Munpacker(reply)
- dumpM(u)
-
-
- # Run test program when called as a script
-
- if __name__ == '__main__':
- test()
-